﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;

namespace MvvmFx.Common.Helpers
{
    public static partial class DispatcherHelper
    {
        /// <summary>
        /// Returns the current Dispatcher for the GUI thread
        /// </summary>
        public static Dispatcher GetCurrent()
        {
            try
            {
                return Application.Current.RootVisual.Dispatcher;
            }
            catch (TypeInitializationException)
            {
                // In unit tests this will catch a TypeInitializationException 
                // and return null, as the Application.Current call will throw
                // this exception
                return null;
            }
        }
    }
}
